const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')

function resolve (dir) {
  return path.join(__dirname, dir).replace(/\\/g, '/')
}
var phaserModule = path.join(__dirname, '/node_modules/phaser/');
var phaser = path.join(phaserModule, 'build/custom/phaser-split.js'),
  pixi = path.join(phaserModule, 'build/custom/pixi.js'),
  p2 = path.join(phaserModule, 'build/custom/p2.js');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  cache: {
    type: 'memory'
  },
  module: {
    rules: [
      
      { 
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
      { test: /\.vue$/, use: 'vue-loader' },
      { test: /pixi.js/, loader: "script-loader" },
      { test: /p2.js/, loader: "script-loader" },
    ]
  },
  resolve: {
    alias: {
      'phaser': phaser,
      'pixi.js': pixi,
      'p2': p2,
    }
  },
  devtool: 'inline-source-map',
  devServer: {
    contentBase: './dist',
    open: false,
  },
  plugins:[
    new CopyWebpackPlugin({
      patterns:[
      {
           from: resolve('assets'),	// 原始目录
           to: 'assets',	// 输出目录
       }
    ]}),
    new VueLoaderPlugin()
  ],
  optimization:{
    splitChunks: {

      chunks: "async",
      minSize: 3000, // 模块的最小体积
      minChunks: 1, // 模块的最小被引用次数
      maxAsyncRequests: 5, // 按需加载的最大并行请求数
      maxInitialRequests: 3, // 一个入口最大并行请求数
      automaticNameDelimiter: '~', // 文件名的连接符
      name: 'name',
      cacheGroups: { // 缓存组
          vendors: {
              test: /[\\/]node_modules[\\/]/,
              priority: -10
          },
          default: {
              minChunks: 2,
              priority: -20,
              reuseExistingChunk: true
          }
      }
    }
  }
  
};